﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "mineSweeping.h"

int Time = 0;

void InitBoard(char board[EASY_ROWS][EASY_COLS], int row, int col, char setChar)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = setChar;
		}
	}
}

void DisplayBoard(char board[EASY_ROWS][EASY_COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("< 输入[坐标-x 字符-p 坐标-y] - 标记或取消标记雷点 >\n");
	printf("         [ 时间: %d ] \n", Time);
	for (i = 0; i < col+1; i++)
	{
		printf("%2d ", i);
	}
	printf("\n");

	for (i = 1; i < row+1; i++)
	{
		printf("%2d ", i);
		for (j = 1; j < col+1; j++)
		{
			printf(" %c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");

}

void setMine(char board[EASY_ROWS][EASY_COLS], int row, int col)
{
	const int mineArr[] = { 0, EASY_COUNE, MEDIUM_COUNE,DIFFICULT_COUNE };
	int count = mineArr[mode];//雷的数量

	//while会执行>=count次
	while (count)
	{
		int x = rand() % row + 1;// 1~9行
		int y = rand() % col + 1;// 1~9列
		if (board[x][y] == '0')
		{
			board[x][y] = '1';//设置雷
			count--;
		}
	}

}

static int get_round_mine_count(char mine[EASY_ROWS][EASY_COLS], int x, int y)
{
	return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1]
		+ mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0';
}

static void is_DisplayBoard(char show[EASY_ROWS][EASY_COLS])
{
	if (mode == 1)
	{
		DisplayBoard(show, EASY_ROW, EASY_COL);
	}
	else if (mode == 2)
	{
		DisplayBoard(show, MEDIUM_ROW, MEDIUM_COL);
	}
	else if (mode == 3)
	{
		DisplayBoard(show, DIFFICULT_ROW, DIFFICULT_COL);
	}
}

static int getCheckMaxCount(int row, int col)
{
	int MaxCountArr[] = { 0, row * col - EASY_COUNE,
				row * col - MEDIUM_COUNE,
				row * col - DIFFICULT_COUNE };

	return MaxCountArr[mode];
}

void unfold(char mine[EASY_ROWS][EASY_COLS], char show[EASY_ROWS][EASY_COLS],int x, int y,int row, int col)
{
	if (x <= 0 || y <= 0 || x > row || y > col)
		return;
	int mineCount = get_round_mine_count(mine, x, y);

	if (mineCount == 0)//如果x和y周围一圈的位置等于0,那么递归排查周围没有被排查过的方向
	{
		show[x][y] = ' ';
		if (show[x - 1][y] == '*')
			unfold(mine, show, x - 1, y, row, col);
		if(show[x - 1][y-1] == '*')
			unfold(mine, show, x - 1, y-1, row, col);
		if (show[x][y-1] == '*')
			unfold(mine, show, x, y-1, row, col);
		if (show[x+1][y-1] == '*')
			unfold(mine, show, x+1, y-1, row, col);
		if (show[x+1][y] == '*')
			unfold(mine, show, x+1, y, row, col);
		if (show[x+1][y+1] == '*')
			unfold(mine, show, x + 1, y + 1, row, col);
		if (show[x][y + 1] == '*')
			unfold(mine, show, x, y + 1, row, col);
		if (show[x - 1][y + 1] == '*')
			unfold(mine, show, x - 1, y + 1, row, col);
	}
	else
	{
		show[x][y] = mineCount + '0';
	}
	
	
}

void CheckMine(char mine[EASY_ROWS][EASY_COLS], char show[EASY_ROWS][EASY_COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int checkCount = 0;
	int checkMaxCount = getCheckMaxCount(row, col);

	while (checkCount != checkMaxCount)
	{
		char ch = '0';
		is_DisplayBoard(show);
		printf("输入要排查的坐标[x y]:>");
		scanf("%d%c%d", &x, &ch, &y);
		Time++;
		if (ch == ' ')
		{

			if (mine[x][y] == '1') //是雷 -- 结束游戏
			{
				system("cls");
				printf("很遗憾，你被炸死了!\n");
				is_DisplayBoard(mine);
				Time = 0;
				break;
			}
			else //不是雷，将坐标附近一圈内雷的个数赋值到show数组对应的坐标上
			{
				if (show[x][y] == '*')
				{
					unfold(mine, show, x, y, row, col);//连续展开一片
					//show[x][y] = get_round_mine_count(mine, x, y) + '0';//获取周围一圈雷的个数
					checkCount++;
				}
				else
				{
					system("cls");
					printf("\n该坐标已经排查过了, 无需再次排查\n\n");
				}
			}
		}
		else if (ch == 'p') //判断输入有没有p
		{
			if (show[x][y] != '#')//标记雷
			{
				show[x][y] = '#';
			}
			else
			{
				show[x][y] = '*';//取消标记雷
			}
		}
		else
		{
			system("cls");
			printf("坐标输入格式不正确，请重新输入！\n");
		}
	}

	if(checkCount == checkMaxCount) //除雷以外的地方都被排查完了，游戏通关
	{
		system("cls");
		printf("恭喜你，通过了！！！\n");
		is_DisplayBoard(mine);
		Time = 0;
	}
}